<?php
session_start();
include 'connect_db.php';
$temp    =$_POST['action'];
if($temp=='Commander le panier'){
    //(commander)
    $sql    = "SELECT   db_cart.cartes_idcarte as 'idcarte', 
                        db_cart.qty as 'qty',
                        db_cartes.prix_indique as 'price'
                        FROM db_cart 
                        INNER JOIN db_cartes ON db_cartes.idcarte=db_cart.cartes_idcarte
                        WHERE clients_email='".$myusername."'";
    $result =mysql_query($sql);
    
    $sql2       ="SELECT MAX(idcmdcl) as 'max' FROM db_cmdcl";
    $result2    =mysql_query($sql2);
    while($tableau2=mysql_fetch_array($result2,MYSQL_ASSOC)){
        $temp_cmd   =$tableau2['max'];
    }



    while($tableau=mysql_fetch_array($result,MYSQL_ASSOC)){
        $sql1 = "INSERT INTO db_cmdcl (clients_email,cartes_idcarte,etat,date_commande,date_reception,quantite,prix_unitaire,idcmdcl)
                    VALUES ('".$myusername."',".$tableau['idcarte'].",'enregistre',CURDATE(),DATE_ADD(CURDATE(), INTERVAL 5 DAY),".$tableau['qty'].",".$tableau['price'].",".$temp_cmd."+1)";
        $result1 =mysql_query($sql1);
        //echo $sql1;
    }
    $sql    = "DELETE FROM db_cart WHERE clients_email='".$myusername."'";
    $result =mysql_query($sql);
    header("location:commandes.php");
}else{
    //(effacer panier)
    $sql    = "DELETE FROM db_cart WHERE clients_email='".$myusername."'";
    $result =mysql_query($sql);
    header("location:panier.php");
}

?>
